package zhai;

public class VolatileT {
    // volatile　无法保证操作的原子性
    private volatile static int num = 0;

    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    add();
                }
            }).start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(num);
    }

    public static void add() {
        num++;
    }


}
